<?php
declare (strict_types = 1);

namespace app\api\controller\User;

use app\BaseController;
use app\common\model\Config as ConfigModel;
use app\common\model\User\User as UserModel;
use app\common\model\Region as RegionModel;
use app\common\validate\User\User as UserValiDate;

class Base extends BaseController
{
    // vue商城 POST
    public function register() {
        
        try{
            // 接收全部参数
            $params = $this->request->param();

            // 生成密码盐
            $salt = build_ranstr();

            // md5加密
            $password = md5($params['password'].$salt);

            // 查询注册积分
            $point = ConfigModel::where(['key' => 'RegisterPoint'])->value('value');

            // 封装数据
            $data = [
                'email' => $params['email'],
                'password' => $password,
                'salt' => $salt,
                'point' => $point,
                'sex' => 0,
                'auth' => 0,
                'vip' => 1,
            ];

            // 验证数据
            validate(UserValiDate::class)->scene('register')->check($data);

            // 插入数据表
            $result = UserModel::create($data);

            if($result == FALSE) {
                throw new \Exception ("注册失败");
            }

        } catch(\think\exception\ValidateException $e) {
            // 数据验证不通过时会进入，把错误信息显示
            $this->error($e->getError());
        } catch (\Exception $error) {
            $this->error($error->getMessage());
        }

        $this->success('注册成功');
        
    }

    public function login() {
        $params = $this->request->param();

        $User = UserModel::where('email',$params['email'])->find();

        if(!$User) {
            $this->error('用户不存在，请重新注册');
        }

        $password = md5($params['password'].$User['salt']);

        if($password != $User['password']) {
            $this->error('密码错误，请重新输入');
        }

        $data = [
            'id' => $User['id'],
            'email' => $User['email'],
            'nickname' => $User['nickname'],
            'avatar' => $User['avatar_cdn'],
            'cover' => $User['cover_cdn'],
            'sex' => $User['sex'],
            'sex_text' => $User['sex_text'],
            'province' => $User['province'],
            'city' => $User['city'],
            'district' => $User['district'],
            'createtime' => $User['createtime'],
            'content' => $User['content'],
            'point' => $User['point'],
            'vip' => $User['vip'],
            'auth' => $User['auth'],
            'region_text' => $User['region_text']
        ];

        $this->success('登录成功',null,$data);

    }

    public function check() {
        $userid = $this->request->param('userid');

        $User = UserModel::find($userid);

        if(!$User) {
            $this->error('非法登录');
        }

        $data = [
            'id' => $User['id'],
            'email' => $User['email'],
            'nickname' => $User['nickname'],
            'avatar' => $User['avatar_cdn'],
            'cover' => $User['cover_cdn'],
            'sex' => $User['sex'],
            'sex_text' => $User['sex_text'],
            'province' => $User['province'],
            'city' => $User['city'],
            'district' => $User['district'],
            'createtime' => $User['createtime'],
            'content' => $User['content'],
            'point' => $User['point'],
            'vip' => $User['vip'],
            'auth' => $User['auth'],
            'region_text' => $User['region_text']
        ];

        $this->success('验证成功',null,$data);

    }

    public function profile() {
        $params = $this->request->param();

        $User = UserModel::find($params['userid']);

        if(!$User) {
            $this->error('用户不存在');
        }

        $data = [
            'nickname' => $params['nickname'],
            'sex' => $params['sex'],
            'content' => $params['content']
        ];

        // 判断$params['code']是否存在
        if(isset($params['code'])) {
            $parentpath = RegionModel::where(['code' => $params['code']])->value('parentpath');

            if(!$parentpath) {
                $this->error('所选地区不存在');
            }

            [$province,$city,$district] = explode(',',$parentpath);
            
            $data['province'] = $province;

            $data['city'] = $city;

            $data['district'] = $district;
        }

        // 判断$params['password']是否存在
        if(isset($params['password'])) {
            $salt = build_ranstr();

            $data['password'] = md5($params['password'].$salt);

            $data['salt'] = $salt;
        }

        if(isset($_FILES['avatar']) && $_FILES['avatar']['size'] > 0) {
            $avatar = upload('avatar');

            if($avatar['code'] === 0) {
                $this->error($avatar['msg']);
            }

            $data['avatar'] = $avatar['data'];

        }

        if(isset($_FILES['cover']) && $_FILES['cover']['size'] > 0) {
            $cover = upload('cover');

            if($cover['code'] === 0) {
                $this->error($cover['msg']);
            }

            $data['cover'] = $cover['data'];

        }

        $result = UserModel::update($data,['id' => $User['id']]);

        if($result !== FALSE) {
            if(isset($data['avatar']) && $_FILES['avatar']['size'] > 0) {
                @is_file((String)$User['avatar']) && @unlink($User['avatar']);
            }
            if(!empty($data['cover']) && $_FILES['cover']['size'] > 0) {
                @is_file((String)$User['cover']) && @unlink($User['cover']);
            }

            $user = UserModel::find($params['userid']);

            $UserData = [
                'id' => $user['id'],
                'email' => $user['email'],
                'nickname' => $user['nickname'],
                'avatar' => $user['avatar_cdn'],
                'cover' => $user['cover_cdn'],
                'sex' => $user['sex'],
                'sex_text' => $user['sex_text'],
                'province' => $user['province'],
                'city' => $user['city'],
                'district' => $user['district'],
                'createtime' => $user['createtime'],
                'content' => $user['content'],
                'point' => $user['point'],
                'vip' => $user['vip'],
                'auth' => $user['auth'],
                'region_text' => $user['region_text']
            ];

            $this->success('更新成功',null,$UserData);
        }else {
            if(!empty($data['avatar']) && $_FILES['avatar']['size'] > 0) {
                @is_file((String)$data['avatar']) && @unlink($data['avatar']);
            }
            if(!empty($data['cover']) && $_FILES['avatar']['size'] > 0) {
                @is_file((String)$data['cover']) && @unlink($data['cover']);
            }

            $this->error('更新失败');
        }
    }

}
